using MapMaker.VirtualEarth;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MapMaker
{

    public partial class frmProgress : Form
    {

        private delegate void setStatusDelegate(string message);
        private delegate void setProgressDelegate(int progress);

        private enum ProgressType
        {
            MapDownload,
            GPSFS
        }

        public int LowerBound
        {
            get
            {
                return pbaProgress.Minimum;
            }
            set
            {
                pbaProgress.Minimum = value;
            }
        }

        public int UpperBound
        {
            get
            {
                return pbaProgress.Maximum;
            }
            set
            {
                pbaProgress.Maximum = value;
            }
        }

        public int ProgressStep
        {
            get
            {
                return pbaProgress.Step;
            }
            set
            {
                pbaProgress.Step = value;
            }
        }

        public string Status
        {
            set
            {
                if (lblStatus.InvokeRequired)
                {
                    setStatusDelegate d = new setStatusDelegate(setStatus);
                    this.Invoke(d, new object[] { value });
                }
                else
                {
                    lblStatus.Text = value;
                }
            }
        }

        public void IncrementProgress(int step)
        {
            if (pbaProgress.InvokeRequired)
            {
                setProgressDelegate d = new setProgressDelegate(IncrementProgress);
                this.Invoke(d, new object[] { step });
            }
            else
            {
                pbaProgress.Increment(step);
            }
        }

        public void SetProgress(int value)
        {
            if (pbaProgress.InvokeRequired)
            {
                setProgressDelegate d = new setProgressDelegate(SetProgress);
                this.Invoke(d, new object[] { value });
            }
            else
            {
                pbaProgress.Value = value;
            }
        }

        public frmProgress()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Are you sure you want to cancel this task?", "Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                if (Program.WorkerThread != null)
                {
                    Program.WorkerThread.Abort();
                    Program.WorkerThread.Join();
                }

                if (Program.PrimaryDownloadWorker != null)
                {
                    Program.PrimaryDownloadWorker.Abort();
                    Program.PrimaryDownloadWorker.Join();
                }

                if (Program.SecondaryDownloadWorker != null)
                {
                    Program.SecondaryDownloadWorker.Abort();
                    Program.SecondaryDownloadWorker.Join();
                }

                this.Close();
            }
        }

        private void setStatus(string message)
        {
            lblStatus.Text = message;
        }
    }
}